package be.phl.obstacleapp;

import java.util.ArrayList;

import be.phl.layoutAdapter.MovementRestrictionAdapter;
import be.phl.model.DBAdapterPatient;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;

public class MovementRestrictionFragment extends Fragment {

	private RelativeLayout layoutMiddle = null;
	private TableLayout layout1;
	private RatingBar rating;
	private TextView text;
	private int PatientId;
	int i;
	ArrayList<MovementRestrictionAdapter> mr = new ArrayList<MovementRestrictionAdapter>();
	Button btnSave;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstances) {
		return inflater.inflate(R.layout.fragment_movement_restiction,
				container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		layoutMiddle = (RelativeLayout) getActivity().findViewById(
				R.id.functionelebewegingsmogelijkheid);
		layout1 = (TableLayout) getActivity().findViewById(
				R.id.functionelebewegingsmogelijkheidtable);
		btnSave = (Button) getActivity().findViewById(R.id.btnSave);

		btnSave.setOnClickListener(btnOnClickListener);

		PatientId = (int) getActivity().getIntent().getIntExtra("PatientId", 0);
		DBAdapterPatient db = new DBAdapterPatient(getActivity());

		db.open();
		Cursor p = db.getMovementRestriction();

		while (p.moveToNext()) {

			Cursor c = db.getMovementRestrictionPatient(PatientId, p.getInt(0));

			while (c.moveToNext()) {
				MovementRestrictionAdapter movementrestriction = new MovementRestrictionAdapter(
						c.getInt(0), p.getInt(0), p.getString(1), c.getInt(1));
				mr.add(movementrestriction);
			}
		}
		db.close();

		for (final MovementRestrictionAdapter movementrestriction : mr) {
			TableRow tr = new TableRow(getActivity());

			text = new TextView(getActivity());
			text.setText(movementrestriction.getName());
			text.setId(i);
			text.setTextColor(Color.BLACK);
			tr.addView(text);

			rating = new RatingBar(getActivity());
			rating.setStepSize((float) 1.2);
			rating.setNumStars(4);
			rating.setRating(movementrestriction.getValue());
			rating.setMax(4);
			rating.setId(i);

			rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				@Override
				public void onRatingChanged(RatingBar arg0, float arg1,
						boolean arg2) {
					movementrestriction.setValue((int) arg1);
				}
			});

			tr.addView(rating);

			i++;
			RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
					android.view.ViewGroup.LayoutParams.MATCH_PARENT,
					android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
			lay.addRule(RelativeLayout.BELOW, layoutMiddle.getId());
			layout1.addView(tr, lay);
		}
	}

	Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {

			PatientId = (int) getActivity().getIntent().getIntExtra(
					"PatientId", 0);
			DBAdapterPatient db = new DBAdapterPatient(getActivity());
			db.open();

			for (final MovementRestrictionAdapter movementrestriction : mr) {

				if (db.updateMovementRestriction(
						movementrestriction.getDb_id(),
						movementrestriction.getValue())) {
					Context context = getActivity().getApplicationContext();
					CharSequence text = "Functionele bewegingsmogelijkheid "
							+ movementrestriction.getName()
							+ " is succesvol aangepast met waarde "
							+ movementrestriction.getValue();
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				} else {
					Context context = getActivity().getApplicationContext();
					CharSequence text = "Functionele bewegingsmogelijkheid "
							+ movementrestriction.getName()
							+ " kon niet worden aangepast";
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}
			}

			db.close();
		}
	};
}
